package com.atguigu.jxc.controller;

import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.entity.vo.GoodsTypeList;
import com.google.gson.Gson;

/**
 * @author luojia
 * @create 2021-12-31 16:35
 */



@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询当前库存，(根据商品类别，商品编码或名称搜索)
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @PostMapping("listInventory")
    public String listInventory(@RequestParam(value = "page",required = false) Integer page,
                                @RequestParam(value = "rows",required = false) Integer rows,
                                @RequestParam(value = "codeOrName",required = false) String codeOrName,
                                @RequestParam(value = "goodsTypeId",required = false) Integer goodsTypeId){
        String goods = goodsService.listInventory(page,rows,codeOrName,goodsTypeId);
        return goods;
    }

    /**
     * 查询当前库存，(根据商品类别，商品编码或名称搜索)
     * @param page
     * @param rows
     * @param goodsTypeId
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> queryAllGoods(@RequestParam(value = "page",required = false) Integer page,
                                            @RequestParam(value = "rows",required = false) Integer rows,
                                            @RequestParam(value = "goodsName",required = false) String goodsName,
                                            @RequestParam(value = "goodsTypeId",required = false) Integer goodsTypeId){
        List<Goods> goods = goodsService.queryAllGoods(page,rows,goodsName,goodsTypeId);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("rows",goods);
        hashMap.put("total",goods.size());
        return hashMap;
    }
    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @PostMapping("deleteStock")
     public ServiceVO deleteStock(@RequestParam("goodsId") Integer goodsId){
        this.goodsService.deleteStock(goodsId);
        return new ServiceVO(100,"请求成功",null);

     }
    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */
    @PostMapping("saveStock")
    public ServiceVO update(@RequestParam("goodsId") Integer goodsId,@RequestParam(value = "inventoryQuantity",required = false) Integer inventoryQuantity,@RequestParam(value = "purchasingPrice",required = false) double purchasingPrice){
        this.goodsService.updateGoods(goodsId,inventoryQuantity,purchasingPrice);
        return new ServiceVO(100,"请求成功",100);
    }
    /**
     * 有库存商品的展示 可以根据商品名称或者编码查询
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(@RequestParam("page") Integer page,@RequestParam("rows") Integer rows,@RequestParam(value = "nameOrCode",required = false) String nameOrCode){
        HashMap<String, Object> hashMap = new HashMap<>();
        List<Goods> goods = this.goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        hashMap.put("total",goods.size());
        hashMap.put("rows",goods);
        return hashMap;
    }
    /**
     * 无库存商品的展示 可以根据商品名称或者编码查询
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("getNoInventoryQuantity")
    public Map<String,Object>  getNoInventoryQuantity(@RequestParam("page") Integer page,@RequestParam("rows") Integer rows,@RequestParam(value = "nameOrCode" ,required = false) String nameOrCode){
        List<Goods> goods =  this.goodsService.queryNoInventoryQuantityGoods(page,rows,nameOrCode);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total",goods.size());
        hashMap.put("rows",goods);
        return hashMap;
        }
    /**
     * 删除商品
     * @param goodsId
     * @return
     */
    @PostMapping("delete")
    public ServiceVO goodsDelete(@RequestParam("goodsId") Integer goodsId){
        this.goodsService.goodsDelete(goodsId);
        return new ServiceVO(100,"请求成功",null);
    }
    /**
     * 添加或者修改商品
     * @param goods
     * @param goodsId
     * @return
     */
    @PostMapping("save")
    public ServiceVO goodsAddUpdate( Goods goods,@RequestParam(value = "goodsId",required = false) String goodsId){
        this.goodsService.goodsAddUpdate(goods,goodsId);
        return new ServiceVO(100,"请求成功",null);
    }


}
